package com.example.blog.controller;

import com.example.blog.common.pojo.dataobject.UserInfo;
import com.example.blog.common.pojo.request.UserLoginParam;
import com.example.blog.common.pojo.response.UserInfoResponse;
import com.example.blog.common.pojo.response.UserLoginResponse;
import com.example.blog.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    //Bean的名称=类名首字母小写,如果前两位均为大写,那么Bean就是他本身
    //@Autowired是按类型注入，而@Resource默认按名称注入,如果使用 @Autowired 按名称注入，需额外添加 @Qualifier
    @Resource(name="userServiceImpl")
    private UserService userService;

    @RequestMapping("/login")
    //参数是一个json,所以要写@RequestBody用于接受
    //校验实体类前面加注解@Validated
    public UserLoginResponse login(@Validated @RequestBody UserLoginParam userParam){
        log.info("用户登录,userName:{}",userParam.getUserName());
        //和cookie&Session一样,相当与把他换成了token
        return userService.login(userParam);
    }

    @RequestMapping("/getUserInfo")
    public UserInfoResponse getUserInfo(@NotNull Integer userId){
        log.info("获取用户信息,userId:{}",userId);
        return userService.getUserInfoById(userId);
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfoResponse getAuthorInfo(@NotNull Integer blogId){
        log.info("获取作者信息,blogId:{}",blogId);
        return userService.getAuthorInfoById(blogId);
    }
}
